RED HAT ENTERPRISE LINUX

Editing Text Files

Mastering vim and nano for System Administration

CIS126RH | RHEL System Administration 1
Mesa Community College

Learning Objectives

1
Understand vim's modal editing

Navigate between Normal, Insert, and Command modes

2
Perform essential vim operations

Open, edit, save, and exit files efficiently

3
Use vim navigation and editing commands

Move, search, copy, paste, and delete text

4
Use nano for quick edits

Navigate nano as a simpler alternative editor

Why Text Editors Matter

In Linux, configuration files are plain text. System administration means editing files in /etc, writing scripts, and modifying configurations - all through text editors.

vim / vi

Powerful, ubiquitous, available on every Unix system. Modal editing provides efficiency once learned. RHCSA exam standard.

nano

Simple, intuitive, shows shortcuts on screen. Good for beginners and quick edits. Less powerful than vim.

RHCSA Exam: The exam environment has vim available. Knowing vim is essential for completing exam tasks efficiently.

vim vs vi

vi (Original)

  • Created in 1976 by Bill Joy
  • POSIX standard editor
  • Basic modal editing
  • Available on ALL Unix systems
  • Minimal features

vim (Vi IMproved)

  • Created in 1991 by Bram Moolenaar
  • Superset of vi
  • Syntax highlighting, plugins
  • Multiple undo, visual mode
  • Default on most Linux distros
# On RHEL, vi is actually vim
ls -l /usr/bin/vi
lrwxrwxrwx. 1 root root 3 Nov  1 10:00 /usr/bin/vi -> vim

# Check vim version
vim --version | head -1
VIM - Vi IMproved 8.2

vim Modal Editing

Modal editing means vim has different modes for different tasks. Keys do different things depending on the current mode.

NORMAL INSERT COMMAND VISUAL
NORMAL ← vim starts here
i a o

INSERT
Esc to return
:

COMMAND
Esc or Enter
v V

VISUAL
Esc to return

Starting and Exiting vim

# Open vim with a file
vim filename.txt

# Open vim without a file
vim

# Open at specific line number
vim +50 filename.txt        # Opens at line 50

# Open in read-only mode
vim -R filename.txt
view filename.txt           # Same as vim -R
Essential Exit Commands (from Normal mode, type :)
:q Quit (fails if unsaved changes)
:q! Quit without saving (force)
:w Write (save) the file
:wq Write and quit
:x Write and quit (same as :wq)
ZZ Write and quit (Normal mode shortcut)

Entering Insert Mode

Key Action Use When
i Insert before cursor Most common - insert at current position
I Insert at beginning of line Adding content to start of line
a Append after cursor Adding after current character
A Append at end of line Adding content to end of line
o Open new line below Inserting a new line after current
O Open new line above Inserting a new line before current
Remember: Press Esc to return to Normal mode when done typing.

Navigation in Normal Mode

Basic Movement
h j k l← ↓ ↑ →
0Beginning of line
$End of line
^First non-blank char
wNext word
bPrevious word
Jumping
ggGo to first line
GGo to last line
:nGo to line n
Ctrl+fPage forward
Ctrl+bPage backward
%Matching bracket
Arrow Keys: Arrow keys also work for navigation, but h/j/k/l keep your hands on the home row for faster editing.

Basic Editing Commands

Command Action
x Delete character under cursor
X Delete character before cursor (backspace)
dd Delete (cut) entire line
D Delete from cursor to end of line
dw Delete word from cursor
yy Yank (copy) entire line
yw Yank (copy) word
p Paste after cursor
P Paste before cursor
u Undo last change
Ctrl+r Redo (undo the undo)

Search and Replace

# Search forward (from